#!/bin/bash
VERSION=SBTS00_ENB_9999_221018_000005
GDB_PATH=/var/fpwork
EXE=CCSEmDispatcherExe
#EXE=CCSRTDaemonExe
    
LIST="uphwapi-rt_"
#LIST="uphwapi-rt_ l2-lo_ l2-lo-dbg_"
#LIST="uphwapi-rt_ 5gl2hidu_ l2-hi-combined-dbg_ l2-lo_ l2-lo-dbg_ l2-ps_0.0.0 l2-ps-dbg_0.0.0"
    
INDEX=0 # xeon=0  snf=1  marlin=2
  
ARRY=(
'airscale5g-xeon AirScale5G core2-64'
'sm6-snowfish SM6 tremont-64'
'sm7-marlin SM-r7 aarch64')
  
SOC=`echo ${ARRY[$INDEX]}|cut  -d " " -f1`  #取第1列 airscale5g-xeon
SMX=`echo ${ARRY[$INDEX]}|cut  -d " " -f2`  #取第2列 AirScale5G
ARC=`echo ${ARRY[$INDEX]}|cut  -d " " -f3`  #取第3列 core-64
   
cpath=`pwd`
mkdir out
    
if [ $# = 1 ];then
  VERSION=$1
else
  echo "Please input STBS version.Otherwise use default $VERSION "
fi
    
install_gdb()
{
   if [ -f ${GDB_PATH}/gdb-8.3/gdb ]; then
      echo "gdb already installed"
      return
   fi
   echo "Check gdb install start"  
   
   wget -O gdb-installer.sh https://artifactory-espoo2.int.net.nokia.com/artifactory/mn-rpsw-yocto-sdk-gen-release-es-local/$SOC-toolchain-IB023_PS_REL_2022_09_0058.sh
   chmod +x gdb-installer.sh
  ./gdb-installer.sh -d ${GDB_PATH}/gdb-8.3 -y
         
   cd ${GDB_PATH}/gdb-8.3 && ln -s sysroots/x86_64-oesdk-linux/usr/bin/x86_64-pc-linux-gnu/x86_64-pc-linux-gnu-gdb gdb;cd -
   
   rm -f gdb-installer.sh
   echo "Check gdb install Done"
   # ${GDB_PATH}/gdb-8.3/gdb --version
}
   
unpack_core()
{
echo "-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDHzFc7y0OH7v6w
l7DGpg9zh86HcRB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Q7fh1OaueFa7P
fUySmFIGk1fNCxBu3CQzTcS/xrm/yVEKAdwpS9itYnMC9PXON0eqdyimsVtZ8qz9
gLaP9RXIVOyZZhMHWkmuPd1/UgWKReolRCVXpyvdnEf3KjIBJXgPTpYhAoGAXjWT
NsVuRGe5Nyb/1gSLS16amH7ecI2qo00W9LAtRuXOZmFrSJA6Z/csN8V5QidCylPe
oZOD7/Yo7jhe36K9/Y3L0Lguy86ymNc13jxn37YXIBuEws2HBVxz6moMxx/9S2z2
oX0pRfuqWIOaz860jS43Mu+dnJsRim6HBM2QkpECgYAQACt+C6d/2QDgH25KhLjC
bBgLMzRZVRwIiMipNye+MkM7ECxWj0dE8wKJ594zDNto/c8aOVmHXhko4zaZvB/n
7Kh+7J3KqCL6BeVhnw7mPUGJ3Gy6/Uq/s4iIxDAc6Jxu4bIizsA3bmPRoy0VyGuR
w+40pglZqr+FP0nGT6cdyQ==
-----END PRIVATE KEY-----" > out/localkey
openssl smime -decrypt -inform DER -inkey out/localkey -in *.core.xz.p7 -out ./out/core.xz
unxz ./out/core.xz
    
  echo "Unpack core.xz.p7 done"
    
  if [ -f ccs-rt.txz ]; then
     tar xJf ccs-rt.txz -C out
  else
    find out/ -name ccs-rt.txz|xargs -i tar xJf {} -C out
  fi
   
  if [ -f ccs-rt-debug.txz ]; then
     tar xJf ccs-rt-debug.txz -C out/opt/CCS_RT
  else
    find out/ -name ccs-rt-debug.txz|xargs -i tar xJf {} -C out/opt/CCS_RT
  fi
   
  mv out/opt/CCS_RT/libCCSRT.so.optdebug out/usr/lib64
  echo "========================="
  echo "Unpack CCSRT...Done"
  echo "CCS-RT VERSION out/opt/CCS_RT/version" >>  version-info
  cat out/opt/CCS_RT/version  >>  version-info
  echo "========================="
     
}

set_lfsroot()
{
    echo "========================="
    echo  "$0 $BTS_VER
      WFT  https://wft.int.net.nokia.com/SBTS/CI_ENB/builds/$BTS_VER
      ipk download from https://dbg.es-si-s3-z2.eecloud.nsn-net.net/${BTS_VER}-$SOC-debug-list.html" >  version-info
    echo "========================="
    PKGURL=https://dbg.es-si-s3-z2.eecloud.nsn-net.net/${BTS_VER}/$SOC/$ARC

    wget $PKGURL/Packages.gz
    if [ -f Packages.gz ]; then
      gunzip Packages.gz
    fi

    if [ -f out/usr/lib64/.debug/libc-2.32.so ]; then
       echo "LFS debug symbols already there"
    else
      #For some rare case, we can not get lfs version from SBTS version
      #We could set LFS_VER next line
      #LFS_VER="IB008_PS_LFS_OS_2022_11_0042"
      if [ ${#LFS_VER} = 0 ]; then
        var=`grep "psrel-sysroot version" Packages |awk '{print $4}'`
        LFS_VER=${var%???} #remove last 3 char
      fi

      # len=28 IB023_PS_LFS_OS_2022_09_0028 -> IB2209023
      # len=29 BB23R1_PS_LFS_OS_2022_10_0027 -> FB2210_BB23R1
      if [[ ${LFS_VER} == MB* ]]; then
         LFS_BR="master"
      else
         year=${LFS_VER#*LFS_OS_20} #remove left
         if [[ ${LFS_VER} == IB* ]]; then
           LFS_BR="IB${year:0:2}${year:3:2}${LFS_VER:2:3}"
         else
           LFS_BR="FB${year:0:2}${year:3:2}_${LFS_VER:0:6}"
         fi
      fi

      echo "WFT LFS   : https://wft.int.net.nokia.com/PS/PS_LFS_REL/builds/$LFS_VER
          LFS misc: http://es-si-s3-z4.eecloud.nsn-net.net/build-artifacts/$LFS_BR/$LFS_VER/index.html
         " >> version-info

      if [ $SOC == sm7-marlin-rt ]; then
          SOC=sm7-marlin
      fi

      LFS_URL=http://es-si-s3-z4.eecloud.nsn-net.net/build-artifacts/$LFS_BR/$LFS_VER/$SMX/images/$SOC

      wget -O cpio.file ${LFS_URL}/${SOC/-/_}-rootfs.cpio.compressed
      zstdcat -d cpio.file -o rootfs.cpio
      cd out; cpio -id "usr/lib*" < ../rootfs.cpio;cd -
      rm cpio.file rootfs.cpio

      wget -O debug.file ${LFS_URL}/$SOC-addon_debug-symbols.tar.zst
      zstd -d debug.file -o debug-symbols.tar
      tar -xmf debug-symbols.tar -C out
      rm debug.file debug-symbols.tar
    fi
}


set_lfsroot_old()
{
   if [ -f out/usr/lib64/.debug/libc-2.32.so ]; then
     echo "LFS debug symbols already there"
   else
    var=`grep "psrel-sysroot version" Packages |awk '{print $4}'`
    LFS_VERSION=${var%???} #去掉最后三个字符串(LFS_VERSION=IB023_PS_LFS_OS_2022_09_0028)
    year=${LFS_VERSION:18:2} #取第18 19两个字符 表示年份
    mon=${LFS_VERSION:21:2}  #取第21 22两个字符 表示月份
    ver=${LFS_VERSION:2:3}   #取第3 4两个字符 表示版本
    LFS_BR="IB$year$mon$ver"  #LFS_BR=IB2209023
    
    echo "WFT SBTS  : https://wft.int.net.nokia.com/SBTS/CI_ENB/builds/$VERSION
      WFT LFS   : https://wft.int.net.nokia.com/PS/PS_LFS_REL/$LFS_VERSION
      LFS misc: http://es-si-s3-z4.eecloud.nsn-net.net/build-artifacts/$LFS_BR/$LFS_VERSION/index.html
     " >> version-info
   
           
    LFS_URL=http://es-si-s3-z4.eecloud.nsn-net.net/build-artifacts/$LFS_BR/$LFS_VERSION/$SMX/images/$SOC
    ROOTFS=${SOC/-/_}-rootfs.cpio.compressed
   
    wget $LFS_URL/$ROOTFS
    zstdcat -d $ROOTFS -o rootfs.cpio
    cd out; cpio -id "usr/lib64/*" < ../rootfs.cpio;cd -
    rm $ROOTFS rootfs.cpio 
        
    wget $LFS_URL/${SOC}-addon_debug-symbols.tar.zst
    zstd -d ${SOC}-addon_debug-symbols.tar.zst -o debug-symbols.tar
    tar -xmf debug-symbols.tar -C out
    rm ${SOC}-addon_debug-symbols.tar.zst debug-symbols.tar
  fi
}
   
   
download_ipk()
{
    echo "========================="
    echo  "$0 $VERSION
    WFT  https://wft.int.net.nokia.com/SBTS/CI_ENB/builds/$VERSION
   ipk download from https://dbg.es-si-s3-z2.eecloud.nsn-net.net/$VERSION-$SOC-debug-list.html" >  version-info
    echo "========================="
   PKGURL=https://dbg.es-si-s3-z2.eecloud.nsn-net.net/$VERSION/$SOC/$ARC
   
  wget $PKGURL/Packages.gz
  if [ -f Packages.gz ]; then
    gunzip Packages.gz         
    set_lfsroot         
    for i in ${LIST}    
    do
     IPK=`grep ${i} Packages |awk '{print $2}'` #get name from Packages.gz
     if [ -f  out/${IPK} ]; then
        echo "File ${IPK} has been downloaded"    
     else
       echo "Download ${IPK} from repo"
       wget $PKGURL/${IPK}
       if [ -f  ${IPK} ]; then
           ar -x ${IPK}
           tar -xvmf data.tar.xz -C out
           mv ${IPK} out
           rm -f data.tar.xz control.tar.gz debian-binary
       fi
     fi
    done    
    rm -f Packages
  else
    echo "No pacckages for $VERSION"
  fi
}
    
install_gdb
download_ipk
unpack_core
   
${GDB_PATH}/gdb-8.3/gdb out/opt/CCS_RT/$EXE -c out/core -ex "set solib-search-path out/usr/lib64" -ex "set sysroot ${cpath}/out"